package class07;

/**
 * @Auther: xw
 * @Create: 2022/3/29 14:24
 * @Description: 测试链接：https://leetcode.com/problems/path-sum  能否组成路径和
 */
public class PathSum {
    public static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }
    }
    public static boolean isSum = false;
    public static boolean hasPathSum(TreeNode root, int targetSum) {
        if(null == root){
            return false;
        }
        isSum = false;
        precess(root,0,targetSum);
        return isSum;
    }

    public static void precess(TreeNode root, int preSum, int targetSum) {
        if(null == root.left  && null == root.right){
            if(targetSum == (preSum+ root.val)){
                isSum = true;
            }
           return;
        }
        //root不是叶子节点
        preSum += root.val;
        if(null != root.left){
            precess(root.left,preSum,targetSum);
        }
        if(null != root.right){
            precess(root.right,preSum,targetSum);
        }
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1);
        treeNode.left = new TreeNode(2);
        treeNode.left.left = new TreeNode(3);
        hasPathSum(treeNode,5);
    }


}
